package com.liuzu.esys.controller;

import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.common.R;
import com.liuzu.esys.pojo.Principal;
import com.liuzu.esys.service.PrincipalService;
import com.liuzu.esys.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @Description :天干物燥，小心火烛
 * @Author : yyh
 * @Date : 2022/07/25 15:07:48
 * @desc
 */
@RestController
@RequestMapping("/principal")
public class PrincipalController {
    @Autowired
    private PrincipalService principalService;

    @RequestMapping("/principalList")
    public LayuiPageVo<Principal> principalPageData(@RequestParam Map<String,Object> params){
        return principalService.principalPageData(params);
    }
    @RequestMapping("/deletePrincipal")
    public R deletePrincipal(Integer id){
        int result = principalService.deletePrincipal(id);
        if(result==1){
            return R.ok();
        }else{
            return R.error();
        }
    }
//    @GetMapping("/deletePrincipalByIds")
//    public R deletePrincipalByIds(String ids){
//        int result = principalService.deletePrincipalByIds(ids);
//        if(result>0){
//            return R.ok();
//        }else{
//            return R.error();
//        }
//    }
    @RequestMapping("/updatePrincipal")
    public R updatePrincipal(Principal principal){
        int result = principalService.updatePrincipal(principal);
        if(result==1){
            return R.ok();
        }else{
            return R.error();
        }
    }
    //上传图片
    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        if (file != null) {
            //得到文件名
            String originalFilename = file.getOriginalFilename();
            //将文件转换为流对象
            InputStream inputStream = file.getInputStream();
            //oss加载流文件和名称将其暴露为一个url地址
            String url = OssUtil.uploadFile(inputStream, originalFilename);
            //把文件url传给前端
            return R.ok().data("url", url);
        }
        //如果为空则说明预检请求已经完成相关操作，此时只需要对正式请求放行即可
        return R.ok();
    }
    @RequestMapping("/addPrincipal")
    public R addPrincipal(Principal principal){
        int result = principalService.addPrincipal(principal);
        if(result==1){
            return R.ok();
        }else{
            return R.error();
        }

    }
}
